﻿#region Unimportant using statements

using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

#endregion

namespace Silverlight.Tester
{
    using ToolTip = Controls.ToolTip;
    using ToolTipService = Controls.ToolTipService;

    public partial class Page
    {

        private ToolTip _redToolTip;

        public Page()
        {
            InitializeComponent();

            var person = new Person
                             {
                                 FirstName = "Xavier",
                                 LastName = "Decoster"
                             };
            DataContext = person;

            //BEGIN - Add the tooltip for the red button (duration=persistent; delay=0)
            CorrectMethod();
            //AnotherCorrectMethod();
            //END

            //WrongMethod();

            #region You can ignore this region, it's for demo UI purposes only
            InitializeMyComponents();
            #endregion            
        }

        private void AnotherCorrectMethod()
        {
            _redToolTip = new ToolTip { Content = "1111" };
            _redToolTip.SetValue(ToolTip.ShowDurationProperty, 30);
            _redToolTip.SetValue(ToolTip.InitialDelayProperty, 1);
            RedButton.SetValue(ToolTipService.ToolTipProperty, _redToolTip);
        }
        private void CorrectMethod()
        {
            _redToolTip = new ToolTip
            {
                InitialDelay = 0,
                ShowDuration = int.MaxValue,
                Content = "This is a persistent ToolTip."
            };
            ToolTipService.SetToolTip(RedButton, _redToolTip);
        }
        private void WrongMethod()
        {
            RedButton.SetValue(ToolTipService.ToolTipProperty, "1111");
            RedButton.SetValue(ToolTip.ShowDurationProperty, 30);
            RedButton.SetValue(ToolTip.InitialDelayProperty, 1);
        }

        #region You can ignore this region, it's for demo UI purposes only

        private DispatcherTimer _delayTimer;
        private DispatcherTimer _durationTimer;
        private int _delayTick;
        private int _durationTick;
        private const string DelayFormat = "Delay = {0} seconds";
        private const string DurationFormat = "Duration = {0} seconds";

        private void InitializeMyComponents()
        {
            _delayTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) };
            _durationTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) };
            _delayTimer.Tick += OnDelayTimerTick;
            _durationTimer.Tick += OnDurationTimerTick;
            greenTooltip.Opened += OnToolTipOpened;
            _redToolTip.Opened += OnToolTipOpened;
            greenTooltip.Closed += OnToolTipClosed;
            _redToolTip.Closed += OnToolTipClosed;

            VersionField.Text = "Silverlight.Controls.dll, Version= 1.5.0";
        }

        private void OnToolTipOpened(object sender, RoutedEventArgs e)
        {
            _durationTimer.Start();
            if (((ToolTip)sender).InitialDelay.Equals(0))
                ResetDelayLabel();
            _delayTimer.Stop();
        }
        private void OnToolTipClosed(object sender, RoutedEventArgs e)
        {
            if (_durationTimer.IsEnabled)
                _durationTimer.Stop();
        }
        private void OnDelayTimerTick(object sender, EventArgs e)
        {
            UpdateDelayLabel();
            _delayTick++;
        }
        private void OnDurationTimerTick(object sender, EventArgs e)
        {
            UpdateDurationLabel();
            _durationTick++;
        }
        private void OnButtonClick(object sender, RoutedEventArgs e)
        {
            ((Button)sender).IsEnabled = false;
            ResetTimers();
        }
        private void OnButtonMouseEnter(object sender, MouseEventArgs e)
        {
            ResetDelayLabel();
            ResetDurationLabel();
            _delayTimer.Start();
        }
        private void OnButtonMouseLeave(object sender, MouseEventArgs e)
        {
            ResetTimers();
        }

        private void ResetTimers()
        {
            if (_delayTimer.IsEnabled)
                _delayTimer.Stop();
            if (_durationTimer.IsEnabled)
                _durationTimer.Stop();
        }

        private void OnEnableButtonsClick(object sender, RoutedEventArgs e)
        {
            if (!RedButton.IsEnabled)
                RedButton.IsEnabled = true;
            if (!GreenButton.IsEnabled)
                GreenButton.IsEnabled = true;
        }

        private void ResetDelayLabel()
        {
            _delayTick = 0;
            UpdateDelayLabel();
        }
        private void ResetDurationLabel()
        {
            _durationTick = 0;
            UpdateDurationLabel();
        }
        private void UpdateDelayLabel()
        {
            labelDelay.Dispatcher.BeginInvoke(() => labelDelay.Text = string.Format(DelayFormat, _delayTick));
        }
        private void UpdateDurationLabel()
        {
            labelDuration.Dispatcher.BeginInvoke(() => labelDuration.Text = string.Format(DurationFormat, _durationTick));
        }

        private void OnLinkMouseEnter(object sender, MouseEventArgs e)
        {
            ((TextBlock)sender).TextDecorations = TextDecorations.Underline;
        }
        private void OnLinkMouseLeave(object sender, MouseEventArgs e)
        {
            ((TextBlock)sender).TextDecorations = null;
        }
        private void OnLinkMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var targetLocation = new Uri("http://tooltipservice.codeplex.com", UriKind.Absolute);
            if (HtmlPage.IsPopupWindowAllowed)
            {
                var options = new HtmlPopupWindowOptions { Width = 800, Height = 600 };
                options.Left = (int)(ActualWidth - options.Width) / 2;
                options.Top = (int)(ActualHeight - options.Height) / 2;
                HtmlPage.PopupWindow(targetLocation, "CodePlexWindow", options);
            }
            else HtmlPage.Window.Navigate(targetLocation);
        }

        #endregion

    }
}
